using namespace std;

#include <iostream>
#include "signal.hpp"
#include "main.hpp"

#define SLEEP_TIME 100*1000 //100 millisecond , used by usleep()

/* xstub_p : The global pointer of main object for global program */
xstub * xstub_p = NULL;

int main( int argc , char ** argv ){
	try{ 

		SAY( DP_DEBUG , "Initializing global program !\n" );
		initialize();
	
		SAY( DP_DEBUG , "Create a xstub object !\n" );
		xstub_p = new xstub();
		if( NULL == xstub_p ){
			SAY( DP_DEBUG , "Can't allocate memory for a xstub object, Please try again !!\n ");
			throw;
		}
	
		do{
			SAY( DP_DEBUG , "Xstub oject initializing !\n" );
			if( FALSE == xstub_p->initialize() ){
				SAY( DP_DEBUG , "Xstub_p->initialize failed !\n" );
				break;
			}
			
			SAY( DP_DEBUG , "Initialization succeed, enter main loop of xstub now !\n" );
			xstub_p->run(); 
	
			SAY( DP_DEBUG, "Quit main loop of xstub, uninitialize xstub now !\n" );
			xstub_p->uninit();
			
			usleep( SLEEP_TIME );
	
		}while( !xstub_p->is_quit() );
	
		SAY( DP_DEBUG , "Destroy the xstub object !\n");
		delete xstub_p;
		xstub_p = NULL;
	
		SAY( DP_DEBUG, "Cleanup global program !\n" );
		cleanup();

	}catch( ... ){

		SAY( DP_DEBUG , "Program Failed!\n");
		return -1;
	}
	return 0;
}
